package com.year_23.april;

/**
 * @author: jsh
 * @create: 2023/4/26 21:40
 * @Description:  1031 两个非重叠子数组的最大和
 *                  https://leetcode.cn/problems/maximum-sum-of-two-non-overlapping-subarrays/
 *
 * @FileName: Day26
 * @History:
 * @自定义内容：
 */
public class Day26 {
    public static void main(String[] args) {
//        maxSumTwoNoOverlap()
    }
    public static int maxSumTwoNoOverlap(int[] nums, int firstLen, int secondLen) {
        return Math.max(getMax(nums, firstLen, secondLen), getMax(nums, secondLen, firstLen));
    }

    public static int getMax(int[] nums, int firstLen, int secondLen){
        int suml = 0;
        for (int i = 0; i < firstLen; i++) {
            suml += nums[i];
        }
        int sumr = 0;
        for (int i = firstLen; i < firstLen+secondLen; i++) {
            sumr += nums[i];
        }
        int res = suml + sumr;
        int sumlMax = suml;
        for (int i = 0, j = firstLen+secondLen; j < nums.length; i++,j++) {
            suml = suml - nums[i] + nums[i+firstLen];
            sumlMax = Math.max(suml,sumlMax);
            sumr = sumr - nums[j-secondLen]+nums[j];
            res = Math.max(res,sumlMax+sumr);

        }
        return res;

    }
}
